/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import sys.bean.SerialBean;
import sys.useful.Encrypter;
import sys.useful.Msg;
import sys.useful.Params;
import sys.useful.Utils;
import sys.useful.Validations;

/**
 *
 * @author KERMES
 */
public class TelaSerial extends javax.swing.JFrame {

    /**
     * Creates new form TelaSerial
     */
    private int dias = 30;
    private SerialBean objSerial = new SerialBean();
    private String inicio = "SIM";
    private NumberFormat formatoMoeda = NumberFormat.getCurrencyInstance();

    public TelaSerial(java.awt.Frame parent, boolean modal) {
        initComponents();
        this.jTabbedPane1.setEnabledAt(1, false);
        formatoMoeda.setMinimumFractionDigits(2);
        this.setIconImage(new ImageIcon(getClass().getResource("/sys/image/icone-sistema.png")).getImage());
        txCodigoSerial.requestFocus();
        Utils.centralizar(this);
        Utils.naoRedimensionar(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txCodigoSerial = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txValidadorSerial = new javax.swing.JTextField();
        btnIniciar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txCliente = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txControle = new javax.swing.JTextField();
        txSituacao = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txTipo = new javax.swing.JTextField();
        txMensalidade = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txDataRegistro = new javax.swing.JTextField();
        txDataInspiracao = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txAtualizacao = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnGerar = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        txCliente1 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txSituacao1 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        lbTipo = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txDataRegistro1 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        txDataInspiracao1 = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        txAtualizacao1 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        txNovoSerial = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        txNovoValidador = new javax.swing.JTextField();
        txControle1 = new javax.swing.JComboBox();
        txTipo1 = new javax.swing.JComboBox();
        txMensalidade1 = new javax.swing.JFormattedTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New System Manager - Serial Control");

        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPane1MouseClicked(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel1.setText("CÓDIGO SERIAL*");

        txCodigoSerial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txCodigoSerialKeyTyped(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel2.setText("VALIDADOR DO SERIAL*");

        txValidadorSerial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txValidadorSerialKeyTyped(evt);
            }
        });

        btnIniciar.setText("INICIAR");
        btnIniciar.setPreferredSize(new java.awt.Dimension(110, 30));
        btnIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "DADOS DO SERIAL"));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel3.setText("CLIENTE");

        txCliente.setEnabled(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel4.setText("CONTROLE");

        txControle.setEnabled(false);

        txSituacao.setEnabled(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel5.setText("SITUAÇÃO");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel6.setText("TIPO");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel7.setText("VALOR DA MENSALIDADE");

        txTipo.setEnabled(false);

        txMensalidade.setEnabled(false);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel8.setText("DATA DA INSPIRAÇÃO");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel9.setText("DATA DO REGISTRO");

        txDataRegistro.setEnabled(false);

        txDataInspiracao.setEnabled(false);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel10.setText("ATUALIZAÇÃO");

        txAtualizacao.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txCliente)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel10))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel4))
                                .addGap(0, 103, Short.MAX_VALUE))
                            .addComponent(txTipo)
                            .addComponent(txControle, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txAtualizacao)
                            .addComponent(txDataRegistro, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txDataInspiracao, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txMensalidade)
                                .addComponent(jLabel7)
                                .addComponent(jLabel5)
                                .addComponent(txSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txControle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txMensalidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txDataRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txDataInspiracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txAtualizacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txCodigoSerial)
                    .addComponent(txValidadorSerial, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(btnIniciar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txCodigoSerial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txValidadorSerial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnIniciar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("SERIAL ATUAL", jPanel1);

        btnGerar.setText("GERAR");
        btnGerar.setPreferredSize(new java.awt.Dimension(110, 30));
        btnGerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGerarActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel11.setText("CLIENTE");

        txCliente1.setEnabled(false);

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel12.setText("CONTROLE");

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel13.setText("SITUAÇÃO");

        txSituacao1.setText("ATIVO");
        txSituacao1.setEnabled(false);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel14.setText("TIPO");

        lbTipo.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        lbTipo.setText("VALOR DA MENSALIDADE*");

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel16.setText("DATA DO REGISTRO");

        txDataRegistro1.setEnabled(false);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel17.setText("DATA DA INSPIRAÇÃO");

        txDataInspiracao1.setEnabled(false);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel18.setText("ATUALIZAÇÃO");

        txAtualizacao1.setEnabled(false);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "NOVO SERIAL"));

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel19.setText("CÓDIGO SERIAL");

        txNovoSerial.setEditable(false);

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel20.setText("VALIDADOR DO SERIAL");

        txNovoValidador.setEditable(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txNovoSerial)
                    .addComponent(txNovoValidador, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel19)
                            .addComponent(jLabel20))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel19)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txNovoSerial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel20)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txNovoValidador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        txControle1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "LIVRE", "BLOQUEADO" }));
        txControle1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txControle1ActionPerformed(evt);
            }
        });

        txTipo1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MENSAL", "TRIMESTRAL", "SEMESTRAL", "ANUAL" }));
        txTipo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txTipo1ActionPerformed(evt);
            }
        });

        txMensalidade1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getCurrencyInstance())));
        txMensalidade1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txMensalidade1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txMensalidade1FocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txCliente1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel12)
                                            .addComponent(jLabel18)
                                            .addComponent(jLabel11)
                                            .addComponent(jLabel14))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(txControle1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(txTipo1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(6, 6, 6)))
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbTipo)
                                    .addComponent(txMensalidade1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txSituacao1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel13)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txDataRegistro1)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel16)
                                            .addComponent(txAtualizacao1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel17)
                                    .addComponent(txDataInspiracao1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(16, 16, 16))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnGerar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txCliente1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txSituacao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txControle1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(lbTipo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txTipo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txMensalidade1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txDataRegistro1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txDataInspiracao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel18)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txAtualizacao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnGerar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("NOVO SERIAL", jPanel2);

        jMenu1.setText("NewSM");

        jMenuItem1.setText("Sobre");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txControle1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txControle1ActionPerformed
        txControle1.setBackground(Params.COR_COMBOBOX_NORMAL);
        if (txControle1.getSelectedIndex() == 0) {
            txTipo1.setEnabled(false);
            txTipo1.setSelectedIndex(0);
            lbTipo.setText("VALOR DA MENSALIDADE");
            txMensalidade1.setText(formatoMoeda.format(Integer.parseInt("0")));
            txMensalidade1.setEnabled(false);
        } else {
            txTipo1.setEnabled(true);
            txTipo1.setSelectedIndex(0);
            lbTipo.setText("VALOR DA MENSALIDADE*");
            txMensalidade1.setText(txMensalidade.getText());
            txMensalidade1.setEnabled(true);
        }
    }//GEN-LAST:event_txControle1ActionPerformed

    private void txTipo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txTipo1ActionPerformed
        txTipo1.setBackground(Params.COR_COMBOBOX_NORMAL);
        switch (txTipo1.getSelectedIndex()) {
            case 0:
                dias = 30;
                break;
            case 1:
                dias = 90;
                break;
            case 2:
                dias = 180;
                break;
            case 3:
                dias = 360;
                break;
        }
        if (!inicio.equals("SIM")) {
            verificarInspiracao();
        }
    }//GEN-LAST:event_txTipo1ActionPerformed

    private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
         this.iniciar();
    }//GEN-LAST:event_btnIniciarActionPerformed

    private void setSerial(JTextField serial, JTextField validador) throws Exception {
        String arraySerial[] = new String[5];
        String arrayValidador[] = new String[4];

        String codSerial = Encrypter.decrypt(serial.getText().trim());
        String codValidador = Encrypter.decrypt(validador.getText().trim());

        arraySerial = codSerial.split("_");
        arrayValidador = codValidador.split("_");

        if (!arraySerial[0].equals(arrayValidador[0])) {
            throw new Exception("");

        } else {
            
            Date teste = Utils.convertData(arraySerial[1]);

            objSerial.setCliente(arraySerial[0]);
            objSerial.setDataregistrada(arraySerial[1]);
            objSerial.setDatainspiracao(arraySerial[2]);
            objSerial.setSituacao(arraySerial[3]);
            objSerial.setAtualizacao(arraySerial[4]);

            objSerial.setControle(arrayValidador[1]);
            objSerial.setTipo(arrayValidador[2]);
            objSerial.setMensalidade(arrayValidador[3]);

        }

    }

    private SerialBean getSerial() {
        return objSerial;
    }

    private void iniciar() {
        boolean opc = true;
        String msg = "";
        if (Validations.textField_IsEmpty(txCodigoSerial, opc)) {
            msg += "- Informe o serial atual do cliente\n";
            opc = false;
        }

        if (Validations.textField_IsEmpty(txValidadorSerial, opc)) {
            msg += "- Informe o validador do serial do cliente\n";
            opc = false;
        }

        if (!opc) {
            Msg.alertaValidacao(this, msg);
            return;
        }

        try {

            setSerial(txCodigoSerial, txValidadorSerial);

        } catch (Exception e) {
            txCodigoSerial.setBackground(Params.COR_CAMPO_VAZIO);
            txValidadorSerial.setBackground(Params.COR_CAMPO_VAZIO);
            msg += "- validador ou serial inválido\n";
            Msg.alertaValidacao(this, msg);
            return;
        }

        txCodigoSerial.setEnabled(false);
        txValidadorSerial.setEnabled(false);
        btnIniciar.setEnabled(false);

        limparCampos();
        preencherCamposDadosDoSerial(this.getSerial());
        this.jTabbedPane1.setEnabledAt(1, true);
        inicio = "NAO";
        preencherCamposNovoSerial();
    }

    private void limparCampos() {
        txCliente.setText("");
        txControle.setText("");
        txSituacao.setText("");
        txTipo.setText("");
        txMensalidade.setText("");
        txDataRegistro.setText("");
        txDataInspiracao.setText("");
        txAtualizacao.setText("");

        txCliente1.setText("");
        txControle1.setSelectedIndex(0);
        txTipo1.setSelectedIndex(0);
        txMensalidade1.setText(null);
        txDataRegistro1.setText("");
        txDataInspiracao1.setText("");
        txAtualizacao1.setText("");
        txNovoSerial.setText("");
        txNovoValidador.setText("");

    }

    private void preencherCamposNovoSerial() {
        txCliente1.setText(txCliente.getText());
        txControle1.setSelectedItem(txControle.getText());
        txTipo1.setSelectedItem(txTipo.getText());
        txMensalidade1.setText(txMensalidade.getText());
        txDataRegistro1.setText(txDataInspiracao.getText());
        verificarInspiracao();
        int atu = Integer.parseInt(txAtualizacao.getText().trim()) + 1;
        txAtualizacao1.setText(atu + "");
    }

    private void verificarInspiracao() {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(Utils.convertData(txDataInspiracao.getText()));
        cal.add(Calendar.DATE, dias);
        txDataInspiracao1.setText(Utils.convertData(cal.getTime()));
    }

    private void preencherCamposDadosDoSerial(SerialBean s) {
        txCliente.setText(s.getCliente());
        txControle.setText(s.getControle());
        txSituacao.setText(s.getSituacao());
        txTipo.setText(s.getTipo());
        txMensalidade.setText(s.getMensalidade());
        txDataRegistro.setText(s.getDataregistrada());
        txDataInspiracao.setText(s.getDatainspiracao());
        txAtualizacao.setText(s.getAtualizacao());
    }


    private void txCodigoSerialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txCodigoSerialKeyTyped
        txCodigoSerial.setBackground(Params.COR_CAMPO_NORMAL);
    }//GEN-LAST:event_txCodigoSerialKeyTyped

    private void txValidadorSerialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txValidadorSerialKeyTyped
        txValidadorSerial.setBackground(Params.COR_CAMPO_NORMAL);
    }//GEN-LAST:event_txValidadorSerialKeyTyped

    private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MouseClicked
        switch (jTabbedPane1.getSelectedIndex()) {
            case 0:
                this.txCodigoSerial.requestFocus();
                return;
            case 1:
                this.txControle1.requestFocus();
        }
    }//GEN-LAST:event_jTabbedPane1MouseClicked

    private void txMensalidade1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txMensalidade1FocusLost
        try {
            txMensalidade1.setText(formatoMoeda.format(Double.parseDouble(txMensalidade1.getText().trim().replace("R$ ", "").replace(".", "").replace(",", "."))));
        } catch (NumberFormatException e) {

        }
    }//GEN-LAST:event_txMensalidade1FocusLost

    private void txMensalidade1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txMensalidade1FocusGained
        txMensalidade1.selectAll();
    }//GEN-LAST:event_txMensalidade1FocusGained

    private void btnGerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGerarActionPerformed
        verificarSenha();
    }//GEN-LAST:event_btnGerarActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        TelaSobre so = new TelaSobre(null, true);
        so.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void verificarSenha() {
        TelaSenha senha = new TelaSenha(this, true);
        senha.setVisible(true);
    }

    protected void gerarSerial() {
        txNovoSerial.setText("");
        txNovoValidador.setText("");
        try {
            
            String serial = Encrypter.encrypt(
                    txCliente1.getText().trim() + "_"
                    + txDataRegistro1.getText().trim() + "_"
                    + txDataInspiracao1.getText().trim() + "_"
                    + txSituacao.getText().trim() + "_"
                    + txAtualizacao1.getText()
            );
            
            String validador = Encrypter.encrypt(
                    txCliente1.getText().trim() + "_"
                    + txControle1.getSelectedItem().toString().trim() + "_"
                    + txTipo1.getSelectedItem().toString().trim() + "_"
                    + txMensalidade1.getText().trim()
            );
            
            txNovoSerial.setText(serial.replace("  ", ""));
            txNovoValidador.setText(validador.replace("  ", ""));
        
        } catch (Exception ex) {
            Logger.getLogger(TelaSerial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaSerial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaSerial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaSerial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaSerial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TelaSerial dialog = new TelaSerial(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setDefaultLookAndFeelDecorated(true);
                Utils.setLookAndFeel(dialog);
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGerar;
    private javax.swing.JButton btnIniciar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lbTipo;
    private javax.swing.JTextField txAtualizacao;
    private javax.swing.JTextField txAtualizacao1;
    private javax.swing.JTextField txCliente;
    private javax.swing.JTextField txCliente1;
    private javax.swing.JTextField txCodigoSerial;
    private javax.swing.JTextField txControle;
    private javax.swing.JComboBox txControle1;
    private javax.swing.JTextField txDataInspiracao;
    private javax.swing.JTextField txDataInspiracao1;
    private javax.swing.JTextField txDataRegistro;
    private javax.swing.JTextField txDataRegistro1;
    private javax.swing.JTextField txMensalidade;
    private javax.swing.JFormattedTextField txMensalidade1;
    private javax.swing.JTextField txNovoSerial;
    private javax.swing.JTextField txNovoValidador;
    private javax.swing.JTextField txSituacao;
    private javax.swing.JTextField txSituacao1;
    private javax.swing.JTextField txTipo;
    private javax.swing.JComboBox txTipo1;
    private javax.swing.JTextField txValidadorSerial;
    // End of variables declaration//GEN-END:variables
}
